using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    private static CacheItemRemovedCallback onRemove = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
        BikeReviewWebService.Service bikeReview = new BikeReviewWebService.Service();
        string reviewsVersion = (string)Cache["ReviewsVersion"];
        if (reviewsVersion == null)
        {
            reviewsVersion = bikeReview.GetVersion();
            Cache.Insert("ReviewsVersion", reviewsVersion, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, onRemove);
        }
        string[] bikes = (string[])Cache["BikeList"];
        if (bikes == null)
        {
            bikes = bikeReview.GetBikes();
            string[] sDependencies = new string[1];
            sDependencies[0] = "ReviewsVersion";
            CacheDependency dependency = new CacheDependency(null, sDependencies);
            Cache.Insert("BikeList", bikes, dependency);       
        }
        if (!IsPostBack)
        {
            foreach (string bike in bikes)
            {
                ddBike.Items.Add(bike);
            }
        }
    }
    protected void Page_Prerender(object sender, EventArgs e)
    {
        string sFavsHTML = "<p>You are considering the following bikes:</p><p><b>";
        ArrayList alFavorites = (ArrayList)Session["FavBikes"];
        if (alFavorites != null)
        {
            foreach (string sItem in alFavorites)
            {
                sFavsHTML += sItem + "<br />";
            }
        }
        else
        {
            sFavsHTML += "None";
        }
        sFavsHTML += "</b></p>";
        lblConsidering.Text = sFavsHTML;
    }
    protected void ddBike_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblReview.Text = GetReview(ddBike.Text, chkIncludeScore.Checked);
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        lblReview.Text = GetReview(ddBike.Text, chkIncludeScore.Checked);
    }
    private string GetReview(string Model, bool IncludeScore)
    {
        string htmlResult = "<p>Review: ";
        BikeReviewWebService.Service bikeReview = new BikeReviewWebService.Service();
        string sReview = bikeReview.bikeReview(Model);
        htmlResult += "<b>" + sReview + "</b><br />";
        if (IncludeScore)
        {
            string sScore = bikeReview.bikeScore(Model);
            htmlResult += "<br />Score: <b>" + sScore + "</b>"; 
        }
        htmlResult += "</p>";
        return htmlResult;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ArrayList arFavBikes = (ArrayList)Session["FavBikes"];
        if (arFavBikes == null)
        {
            arFavBikes = new ArrayList();
        }
        arFavBikes.Add(ddBike.Text);
        Session["FavBikes"] = arFavBikes;
    }
    protected void btnRecheck_Click(object sender, EventArgs e)
    {
        BikeReviewWebService.Service bikeReviews = new BikeReviewWebService.Service();
        string currentVersion = (string)Cache["ReviewsVersion"];
        if (currentVersion != bikeReviews.GetVersion())
        {
            Cache.Remove("ReviewsVersion");
        }
    }
    public void RemovedCallback(string key, object value, CacheItemRemovedReason callbackReason)
    {
        BikeReviewWebService.Service bikeReviews = new BikeReviewWebService.Service();
        Cache["ReviewsVersion"] = bikeReviews.GetVersion();
        string[] bikeList = bikeReviews.GetBikes();
        Cache["BikeList"] = bikeList;
        ddBike.Items.Clear();
        foreach (string item in bikeList)
            ddBike.Items.Add(item);
    }
}
